package aStar;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class AStarGUI extends JFrame {

	private static final long serialVersionUID = 1L;

	public static final boolean DRAW_NUMBERS = false;

	private Field field;
	private ArrayList<ArrayList<Tile>> path;

	public AStarGUI(Field field, ArrayList<ArrayList<Tile>> path) {
		
		this.field = field;
		this.path = path;
		
		StarPanel pane = new StarPanel();
		pane.setPreferredSize(new Dimension(1100,700));
		setContentPane(new JScrollPane(pane));
		
	} 
	
	private class StarPanel extends JPanel {

		private static final long serialVersionUID = 1L;
		/*
		 *     protected void paintComponent(Graphics g) {
			        super.paintComponent(g);
			        Graphics2D g2 = (Graphics2D)g;
			        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			                            RenderingHints.VALUE_ANTIALIAS_ON);
			        if(ellipses == null)
			            initEllipses();
			        for(int j = 0; j < ellipses.length; j++) {
			            Color color = Color.blue;
			            if(j == selectedIndex)
			                color = Color.red;
			            g2.setPaint(color);
			            g2.draw(ellipses[j]);
			        }
			    }
		 */
		@Override
		protected void paintComponent(Graphics g) {
			
			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			
			int xSize = getWidth();
			int ySize = getHeight();
			int xCount = field.getField().length;

			for(Tile[] column : field.getField()) {
				int yCount = column.length;
				int tileLength = xSize / xCount;
				int tileHeight = ySize / yCount;
				
				int tileSize=Math.min(tileLength, tileHeight);
				
				for(Tile tile : column) {
					float elevation = Math.min((float) tile.tileHight / 20, 1f);
					//If its not walkable the color is red else its the height
					if(tile.isWalkable()) {
						g2.setColor(new Color(elevation, elevation, elevation));
					} else {
						g2.setColor(new Color(44,0,0));
					}
					
					
					int left = tile.tileX * tileSize;
					int top = tile.tileY * tileSize;
					g2.fillRect(left, top, tileSize, tileSize);
					
					//Draw a grid
					g2.setColor(Color.black);
					g2.drawRect(left, top, tileSize, tileSize);
					
					if (Math.round(tile.F) != 0 && DRAW_NUMBERS) {
						Rectangle2D fSize = g2.getFontMetrics().getStringBounds(String.valueOf(Math.round(tile.F)), g);
						Rectangle2D hSize = g2.getFontMetrics().getStringBounds(String.valueOf(Math.round(tile.H)), g);
						
						g2.setColor(Color.red);
						g2.drawString(String.valueOf(Math.round(tile.F)), left +(int) (tileSize - fSize.getWidth()) / 2 , top + g2.getFont().getSize());
						g2.setColor(Color.green);
						g2.drawString(String.valueOf(Math.round(tile.G)), left + 2,top + tileSize-2);
						g2.setColor(Color.blue);
						g2.drawString(String.valueOf(Math.round(tile.H)), left + tileSize - (int)hSize.getWidth() - 2, top + tileSize - 2);
					}
				}
				
				
				//Indicate the paths			
				for(List<Tile> subPath : path) {
					
					//draw rects around path
					g2.setColor(new Color((float)Math.random(), (float)Math.random(), (float)Math.random()));
					for(Tile tile : subPath) {
						int left = tile.tileX * tileSize;
						int top = tile.tileY * tileSize;
						g2.drawRect(left, top, tileSize, tileSize);
					}
					
					//Special rect for start & stop square
					g2.setColor(Color.yellow);
					int left = subPath.get(0).tileX * tileSize;
					int top = subPath.get(0).tileY * tileSize;
					g2.drawRect(left, top, tileSize, tileSize);
					
					left = subPath.get(subPath.size()-1).tileX * tileSize;
					top = subPath.get(subPath.size()-1).tileY * tileSize;
					g2.drawRect(left, top, tileSize, tileSize);

				}
				
				//Special rect for start & stop square
				g2.setColor(Color.yellow);
				int left = Tester.pathfinder.getOrigin().tileX * tileSize;
				int top = Tester.pathfinder.getOrigin().tileY * tileSize;
				g2.drawRect(left, top, tileSize, tileSize);
				
				left = Tester.pathfinder.getDestination().tileX * tileSize;
				top = Tester.pathfinder.getDestination().tileY * tileSize;
				g2.drawRect(left, top, tileSize, tileSize);

			}
		}
	}
}
